package com.wxj.book.cms.controller;

import com.github.pagehelper.PageInfo;
import com.wxj.book.cms.common.ServerResponse;
import com.wxj.book.cms.pojo.Education;
import com.wxj.book.cms.service.EducationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EducationController {
    @Autowired
    private EducationService educationService;

    /**
     * 查询所有学制
     */
    @RequestMapping("education/getAll")
    public String getAllEducation(@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
                                  @RequestParam(value = "pageNum" , defaultValue = "1") Integer PageNum,
                                  Model model){
        PageInfo<Education> pageInfo = educationService.getAll(PageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "education";
    }

    /**
     * 添加或者更新学年制
     */
    @PostMapping("education/updateEducation")
    @ResponseBody
    public ServerResponse updateEducation(Education education) {
        int result = educationService.updateEducation(education);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * 根据id删除
     */
    @RequestMapping("education/deleteById")
    @ResponseBody
    public ServerResponse deleteById(@RequestParam("educationId") Integer educationId) {
        if (educationId == null|| educationId.equals("") || educationId.equals("")) {
            return ServerResponse.createByErrorMessage("删除失败");
        }
        int result = educationService.deleteById(educationId);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
}
